//
//  YSingleViewController.m
//  STL_ Animation
//
//  Created by DJtd on 16/3/30.
//  Copyright © 2016年 STL. All rights reserved.
//

#import "YSingleViewController.h"
#import "AppDelegate.h"
#import "STL.h"
#import "YSingleBaseModel.h"
#import "YSingleTableViewCell.h"
#import "YMVViewController.h"

@interface YSingleViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) STLTableView *tableView;
@property (nonatomic, strong) UISegmentedControl *segmented;
@property (nonatomic, strong) NSArray *typeArr;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, assign) NSInteger typeIndex;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL refresh;

@end

@implementation YSingleViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //
    self.view.backgroundColor = [UIColor blackColor];

    self.navigationItem.title = @"悦单";
    
    self.typeArr = @[@"CHOICE", @"HOT", @"NEW"];
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    self.typeIndex = 0;
    self.page = 0;
    self.refresh = YES;
    [self creatSegement];
    [self creatTableView];
    [self requestWithType:self.typeArr[self.typeIndex]];
}

#pragma mark - creatSegement
- (void)creatSegement {
    self.segmented = [[UISegmentedControl alloc] initWithItems:@[@"精选", @"热门", @"最新"]];
    self.segmented.frame = CGRectMake(5, 0, ScreenWidth - 10, ScreenHeight * 0.045);
    self.segmented.selectedSegmentIndex = 0;
    self.segmented.tintColor = [UIColor colorWithRed:8/255.0 green:146/255.0 blue:114/255.0 alpha:1];
    [self.segmented setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];
    [self.segmented addTarget:self action:@selector(segementedAction:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:self.segmented];
}

#pragma mark - segementedAction
- (void)segementedAction:(UISegmentedControl *)sender {
    self.refresh = YES;
    self.page = 0;
    self.typeIndex = sender.selectedSegmentIndex;
    [self requestWithType:self.typeArr[self.typeIndex]];
    
}

#pragma mark - creatTableView 
- (void)creatTableView {
    self.tableView = [[STLTableView alloc] initWithFrame:CGRectMake(0, self.segmented.bounds.size.height, ScreenWidth, ScreenHeight - self.segmented.bounds.size.height - 60) style:UITableViewStylePlain];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[YSingleTableViewCell class] forCellReuseIdentifier:@"YSingleTableViewCellIdentifier"];
    
    self.tableView.mj_header = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadAction)];
    
    [self.view addSubview:self.tableView];
}
- (void)refreshAction {
    self.refresh = YES;
    [self requestWithType:self.typeArr[self.typeIndex]];
    [self.tableView.mj_header endRefreshing];
}

- (void)loadAction {
    self.refresh = NO;
    self.page += 20;
    [self requestWithType:self.typeArr[self.typeIndex]];
    [self.tableView.mj_footer endRefreshing];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ScreenHeight * 0.27;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YSingleTableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"YSingleTableViewCellIdentifier"];
    tableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
    tableViewCell.ySingleBaseModel = self.dataSource[indexPath.row];
    
    return tableViewCell;
}
#pragma mark - request
- (void)requestWithType:(NSString *)type {
    
    
    [SJAFNetTool GEtNetWithUrl:[NSString stringWithFormat:@"http://mapi.yinyuetai.com/playlist/list.json?D-A=0&category=%@&offset=%ld&size=20", type, self.page] WithBody:nil headerFile:Dictionary response:SJJSON success:^(id result) {
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        if (result) {
            
            if (self.refresh == YES) {
                [self.dataSource removeAllObjects];
            }
            
            for (NSDictionary *dic in result[@"playLists"]) {
                YSingleBaseModel *ySingleBaseModel = [[YSingleBaseModel alloc] initWithDataSource:dic];
                [self.dataSource addObject:ySingleBaseModel];
            }
            [self.tableView reloadData];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD showAlertView:@"" andMessage:@"网络错误"];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YMVViewController *yMVVC = [[YMVViewController alloc] init];
    
    YSingleBaseModel *model = self.dataSource[indexPath.row];
    
    HomePageBaseModel *homeModel = [[HomePageBaseModel alloc] init];
    homeModel._id = model.Id;
    
    yMVVC.homeModel = homeModel;
    [self.navigationController pushViewController:yMVVC animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
